package pogvue.gui;

import pogvue.gui.event.*;

import java.awt.*;


public final class ScaleCanvas extends ControlledCanvas implements AlignViewportListener,
                                                   ColumnSelectionListener,
                                                   FontChangeListener {

    public static int BLOCKHEIGHT = 4;
//  private Image    img;
  private Graphics gg;

//  private int      imgWidth;
//  private int      imgHeight;
  int      xoffset;
  public boolean buffer = true;

  public static final int HEIGHT = 30;

  private boolean paintFlag = false;

  private final AlignViewport av;

  public ScaleCanvas(AlignViewport av,Controller c) {
    this.av         = av;

    setController(c);
  }

  public boolean handleAlignViewportEvent(AlignViewportEvent e) {
      paintFlag = true;
      repaint();
      return true;
  }
  public boolean handleColumnSelectionEvent(ColumnSelectionEvent e) {
    paintFlag = true;
    repaint();
    return true;
  }
  public boolean handleFontChangeEvent(FontChangeEvent e) {
    paintFlag = true;
    repaint();
    return true;
  }
  public void paint(Graphics g) {

   double charWidth  = av.getCharWidth();
   int charHeight = av.getCharHeight();

    Font f = av.getIdFont();


   // if (img == null ||
   //     imgWidth  != getSize().width  ||
   //     imgHeight != getSize().height ||
   //         paintFlag) {

   //System.out.println("Repainting scalecanvas");

   //   imgWidth  = getSize().width;
   //   imgHeight = getSize().height;
   //   img       = createImage(imgWidth,imgHeight);

   //   gg = img.getGraphics();
   //   gg.setColor(Color.white);
   //   gg.fillRect(0,0,imgWidth,imgHeight);



      paintFlag = false;
  //  }
    //if (!buffer) {
      gg = g;
      paintFlag = false;
    //}

    gg.setFont(f);

    //Fill in the background

    gg.setColor(Color.white);
    //gg.fillRect(0,0,imgWidth,imgHeight);
    gg.fillRect(0,0,size().width,size().height);
    int tickSpace;

    int resWidth = av.getEndRes() - av.getStartRes() + 1;

    // We want roughly one tick every 100 pixels

    double rough = getWidth()/100.0;

    double roughtick = resWidth/rough;
  
    int   digits  = (int)(Math.log(roughtick)/Math.log(10));
    int   num     = (int)(roughtick/Math.pow(10,digits));

    if (Math.abs(10-num) < 3) {
        num = 10;
    } else if (Math.abs(5-num) <= 2) {
        num = 5;
    } else {
        num = 2;
    }
    //System.out.println("Dig " + digits + " " + rough + " " + roughtick + " " + getWidth() + " " + num);

    int space = (int)(num * Math.pow(10,digits));

    //Set the text font

    gg.setColor(Color.black);

    int offset      = av.getOffset();
    int startx      = av.getStartRes() + offset;
    int endx        = av.getEndRes() + offset;

    int scalestartx = startx - startx%space + space;

    // Draw the scale numbers

    gg.setColor(Color.black);


    for (int i=scalestartx;i < endx;i+= space) {

      String string = String.valueOf(i);

      gg.drawString(string,(int)((i-startx-1)*charWidth),15);

    }

    //Fill the selected columns
    ColumnSelection cs = av.getColumnSelection();

    int width = (int)charWidth;

    if (width < 3) {
      width = 3;
    }
 
    for (int i=0; i<cs.size(); i++) {
      int sel  = cs.columnAt(i);
      //      System.out.println("Selection = " + sel);
      if ( sel >= startx  && sel <= endx) {
        gg.setColor(Color.red);
        gg.fillRect((int)((sel-startx)*charWidth),30-BLOCKHEIGHT,width,BLOCKHEIGHT);
      }
    }

    // Now the minipog


    if (av.getMinipog() != null) {

      int startres = av.getMinipog().getAlignViewport().getStartRes();
      int endres   = av.getMinipog().getAlignViewport().getEndRes();

      gg.setColor(new Color(255,0,0,80));

      width = (int)((endres-startres+1)*charWidth);

      if (width < 3) {
	width = 3;
      }

      gg.fillRect((int)((startres-av.getStartRes())*charWidth),0,width,40);

    }

    if (buffer) {
//	g.drawImage(img,0,0,this);
    } else {
//	gg = img.getGraphics();
	//buffer = true;
    }
    

  }

  public Dimension getMinimumSize() {
    return new Dimension(500,HEIGHT);
  }

  public Dimension getPreferredSize() {
    return getMinimumSize();
  }

}

